package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.Requirement;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.condition.RequirementCondition;
import cn.b.sky.mes.mm.service.RequirementService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;

/**
 * 
 * <strong>Title : RequirementController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.mm.controller<br>
 * </strong> <strong>Description : </strong>物料需求控制器<br>
 * <strong>Create on : 2016-9-21 上午11:27:55<br>
 * </strong>
 * <p>
 * 
 * @author ZhiPeng.Tong<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人|修改日期|修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/requirement/*")
public class RequirementController extends SecurityController
{

	@Resource
	RequirementService requirementService;

	@Resource
	ECodeService eCodeService;

	@RequestMapping("requirement_list")
	public String requirementList(@ModelAttribute("condition") RequirementCondition condition, Model model, String navTabId)
	{
		condition.setSiteCode(this.getSiteCode());
		if (StringUtils.isEmpty(condition.getOrderField()))
		{
			condition.setOrderField("createDateTime");
		}
		List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.REQUIREMENT_STATUS.getCode(), SecurityContextUtil.getLanguageCode());
		List<Requirement> list = requirementService.SelectRequirementListByCondition(condition);
		int count = requirementService.SelectRequirementCountByCondition(condition);

		condition.setData(list);
		condition.setTotalCount(count);
		model.addAttribute("StatusECode", ecode);
		model.addAttribute("page", condition);
		model.addAttribute(NAVTABID, navTabId);
		return "mm/requirement/requirement_list";
	}

	@RequestMapping("viewRequirement")
	public String viewRequirement(String navTabId, Model model, String requirementNum)
	{
		if (!StringUtils.isBlank(requirementNum))
		{
			List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.REQUIREMENT_STATUS.getCode(), SecurityContextUtil.getLanguageCode());
			Requirement requirement = requirementService.getRequirement(this.getSiteCode(), requirementNum);
			List<Requirement> details = requirementService.selectRequirement(this.getSiteCode(), requirementNum);

			model.addAttribute("details", details);
			model.addAttribute("requirement", requirement);
			model.addAttribute("StatusECode", ecode);
		}
		return "mm/requirement/requirement_view";
	}

	@RequestMapping("cancelStatusECode")
	@ResponseBody
	public DWZResult cancelStatusECode(String navTabId, Model model, String codes, HttpServletRequest request)
	{
		String[] idArr = codes.split(",");
		List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
		int failures = 0;
		for (int i = 0; i < idArr.length; i++)
		{
			String code = idArr[i];
			String message = null;
			message = requirementService.cancelStatusECode(this.getSiteCode(), code);
			boolean flag = true;
			if (!StringUtils.isEmpty(message))
			{
				failures += 1;
				flag = false;
				message = getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), message);
			}
			BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
			reports.add(report);
		}
		if (failures == 0)
		{
			return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
		}
		else
		{
			return batchjobAjaxDone(request, reports);
		}
	}
}